package com.project2.shiro;

import com.alibaba.druid.sql.visitor.functions.If;
import com.project2.bean.po.*;
import com.project2.mapper.MarketAdminMapper;
import com.project2.mapper.MarketPermissionMapper;
import com.project2.mapper.MarketRoleMapper;
import com.project2.mapper.MarketUserMapper;
import com.sun.mail.imap.protocol.ID;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.Year;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @param
 * @author wj
 * @return
 */
@Component
public class AdminRealm extends AuthorizingRealm {
    @Autowired
    MarketAdminMapper marketAdminMapper;
    //做授权的
    @Autowired
    MarketPermissionMapper marketPermissionMapper;
    @Autowired
    MarketUserMapper marketUserMapper;
    @Autowired
    MarketRoleMapper marketRoleMapper;
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //获取权限信息
        MarketAdmin principal = (MarketAdmin) principalCollection.getPrimaryPrincipal();
        Integer[] roleIds = principal.getRoleIds();
        List<String> permissions=new ArrayList<>();
        //找出该用户所有的权限
        for (Integer roleId : roleIds) {
            MarketPermissionExample example = new MarketPermissionExample();
            MarketPermissionExample.Criteria criteria = example.createCriteria();
            criteria.andRoleIdEqualTo(roleId);
            List<MarketPermission> marketPermissions = marketPermissionMapper.selectByExample(example);
            for (MarketPermission marketPermission : marketPermissions) {
                String permission = marketPermission.getPermission();
                //如果为*，那就要找出所有的权限
                if ("*".equals(permission)){
                    List<PermissionPO> permissionPOS = marketRoleMapper.queryPermissionByPid(2);
                    for (PermissionPO permissionPO : permissionPOS) {
                        permissions.add(permission);
                    }
                }else {
                    permissions.add(permission);
                }
            }
        }

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        simpleAuthorizationInfo.addStringPermissions(permissions);

        return simpleAuthorizationInfo;
    }

    //做认证的
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        MarketToken marketToken = (MarketToken) authenticationToken;
        String username = (String) authenticationToken.getPrincipal();
        //admin的认证

        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (username!=null&&!"".equals(username)){
            criteria.andUsernameEqualTo(username);
        }
        List<MarketAdmin> marketAdmins = marketAdminMapper.selectByExample(marketAdminExample);
        if (marketAdmins.size()>0){
            MarketAdmin marketAdmin = marketAdmins.get(0);
            return new SimpleAuthenticationInfo(marketAdmin,marketAdmin.getPassword(),getName());
        }


        return null;
    }
}
